<?php

class States extends Controller{

	function states(){
		parent::Controller();
		$this->load->helper('url');
		$this->load->model('states_model');
	}
	
	function add_new()
	{
		
		$state_name = array('name'=>'state_name','id'=>'state_name','type'=>'text','class'=>'required');
		$this->load->helper('form');
		$this->load->library('form_validation');
		$displayform ='';
		$displayform .='<div id=dashboard ><span>Manage State</span> <a href='.base_url().'/idea_home/dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
		
		$displayform .='<fieldset style="width:50%;"><legend>Add New State</legend>';
		$attr = array('id'=>'new_state');
		$displayform .= form_open('states/add_new',$attr);
		$displayform .= '<table >';
		$displayform .= '<tr><td align=left> State Name:</td>';
		$displayform .= '<td>'.form_input($state_name).'</td>';
		$displayform .= '</tr>';
		$displayform .= '<tr><td align=center colspan=2>'.form_submit('Submit','Create New State').'</td>';
		$displayform .= '</tr>';
		$displayform .= '</table></fieldset>';
		$displayform .=form_close();
		$this->form_validation->set_rules('state_name', 'State Name', 'trim|required|xss_clean');

		$data = array('formdisplay'=>$displayform );

		
		if ($this->form_validation->run() == FALSE)
		{		
			$this->template->add_js('js/jquery-latest.js');	
			$this->template->add_js('js/jquery.validate.js');
			
			$this->template->add_js('$(document).ready(function(){
	 		   $("#new_state").validate();
	  			});
		','embed');
			$this->template->write_view('header','header', $data, True);
			$this->template->write_view('content', 'general');
			$this->template->render();
		}
		else
		{
			
			$check=$this->states_model->insert_state();
			
			redirect('states/list_states/0');
			
		}
	}
	
	function duplicate_state()
	{
		$displayform ='';
		$displayform .='<div id=dashboard ><span>Manage State</span> <a href='.base_url().'/idea_home/dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
		
		$displayform .= "This place already exist,please go back and try for another name!!";
		//$displayform .= '<div align="right"><input type=button value="Go Back" ONCLICK="history.go(-1)" /></div>';
		$data = array('formdisplay'=>$displayform );
		$this->template->write_view('header','header', $data, True);
		$this->template->write_view('content', 'other_general');
		$this->template->render();
	}
//to list states names	
	function list_states()
	{
		
		$this->load->helper('flexigrid');
						
	  	$displayform ='';
	  	$displayform .='<div id=dashboard ><span>Manage State</span> <a href='.base_url().'/idea_home/dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
	  	$displayform .='<div align=right> <a href='.base_url().'states/add_new >[Add New State]</a></div>';
	  	$displayform .='<br/>';
	  	
	  	$displayform .= '<table id="flex1" style="display:none" border=0 align=left ></table>';
	  	$displayform .= '<br/>';
		
		$colModel['state_id'] = array('Sl No',60,TRUE,'center',0);
		$colModel['state_name'] = array('State Name',160,TRUE,'left',2);
		
		$gridParams = array(
			'width' => '800',
			'height' => 400,
			'rp' => 15,
			'rpOptions' => '[10,20,30,40]',
			'pagestat' => 'Displaying:{from} to {to} of {total} items.',
			'blockOpacity' => 0.5,
			'title' => 'List of States',
			'showTableToggleBtn' => true
		);
		
		$grid_js = build_grid_js('flex1',site_url("/ajax/states_list"),$colModel,'state_id','asc',$gridParams);
		
		$data['js_grid'] = $grid_js;
			
		$data = array('formdisplay'=>$displayform,'js_grid'=>$grid_js );	
		$this->template->add_css('css/flexigrid/flexigrid.css');
		//$this->template->add_js('js/flexigride/jquery.pack.js');
		$this->template->add_js('js/flexigride/flexigrid.pack.js');
		
		//ends here....
		$this->template->write_view('header','header', $data, True);
		$this->template->write_view('content', 'other_general');
		$this->template->render();
	  				
		
	}
	
		
	function state_edit($id=0)
	{
		
		$this->load->helper('form');
		$this->load->library('form_validation');
	
		$state_name = $this->states_model->state_edit($id);
		$name = $state_name->result_array();
		$state_name = array('name'=>'state_name','value'=>$name[0]['state_name'],'type'=>'text','class'=>'required');//its already posted so it will not set again here after validati
			
		
		$this->form_validation->set_rules('state_name', 'State Name', 'trim|required|xss_clean');
		$displayform ='';
		$displayform .='<div id=dashboard ><span>Manage State</span> <a href='.base_url().'/idea_home/dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
		
		$displayform .='<fieldset style="width:50%;"><legend>Edit State Name</legend>';
		$attr = array('id'=>'edit_state');
		$displayform .= form_open('states/state_edit',$attr);
		$displayform .= '<input type=hidden name=id value='.$id.' />';
		$displayform .= '<table class="tablestyle">';
		$displayform .= '<tr><td align=left> State Name:</td>';
		$displayform .= '<td>'.form_input($state_name).'</td>';
		$displayform .= '</tr>';
		
		$displayform .= '<tr><td align=center colspan=2>'.form_submit('submit','Save').'</td>';
		$displayform .= '</tr>';
		$displayform .= '</table></fieldset>';
		$displayform .=form_close();
		//$displayform .= '<div align="right"><input type=button value="Go Back" ONCLICK="history.go(-1)" /></div>';
		
		if ($this->form_validation->run() == FALSE)
		{
			$data = array('formdisplay'=>$displayform );
			$this->template->add_js('js/jquery-latest.js');	
			$this->template->add_js('js/jquery.validate.js');
			$this->template->add_js('js/validate_xls.js');
			$this->template->add_js('$(document).ready(function(){
	 		   $("#edit_state").validate();
	  			});
		','embed');
			$this->template->write_view('header','header', $data, True);
			$this->template->write_view('content', 'general');
			$this->template->render();
		}
		else
		{
			$this->states_model->save_edit();
			redirect('states/list_states/0');
			
		}
		
		
	}
	
 }
